home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / 3DObjects / Sphere.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-07  |  4.2 KB  |  161 lines

  1. /* SAS/C: sc Sphere.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a spinning sphere consisting entirely of dots.  The
  4. ** object is pre-calculated then rotated in real time for speed, although
  5. ** things could be much faster than this (use integer math).
  6. */
  7.  
  8. #include <proto/dpkernel.h>
  9. #include <math.h>
  10.  
  11. BYTE *ProgName      = "Spinning Sphere";
  12. BYTE *ProgAuthor    = "Paul Manias";
  13. BYTE *ProgDate      = "January 1998";
  14. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  15. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  16.  
  17. struct GScreen *screen;
  18. struct JoyData *jport1;
  19.  
  20. void Demo(void);
  21.  
  22. struct DotPixel { double X,Y,Z; };
  23.  
  24. #define AMTCOLOURS 32
  25.  
  26. LONG palette[AMTCOLOURS+2] = {
  27.   PALETTE_ARRAY,32,
  28.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  29.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  30.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  31.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  32. };
  33.  
  34. /***********************************************************************************/
  35.  
  36. void main(void)
  37. {
  38.   if (screen = InitTags(NULL,
  39.      TAGS_SCREEN,    NULL,
  40.        GSA_BitmapTags, NULL,
  41.        BMA_Palette,    palette,
  42.        TAGEND, NULL,
  43.      GSA_Attrib,     SCR_DBLBUFFER,
  44.      TAGEND)) {
  45.  
  46.      if (jport1 = Init(Get(ID_JOYDATA),NULL)) {
  47.         Display(screen);
  48.         Demo();
  49.  
  50.      Free(jport1);
  51.      }
  52.   Free(screen);
  53.   }
  54. }
  55.  
  56. /************************************************************************************
  57. ** Longtitude deterimines the position of the dot on the horizontal axis.
  58. ** Latitude determines the position of the dot on the vertical axis.
  59. */
  60.  
  61. #define AMTDOTS 500       /* The amount of dots in the object. */
  62.  
  63. void Demo(void)
  64. {
  65.   struct DotPixel *object; /* Pointer to our 3D object */
  66.   double *sine;            /* Pointer to our sine table */
  67.   double *cosine;          /* Pointer to our cosine table */
  68.   WORD   i;
  69.   WORD   offsetx = (screen->Width/2);
  70.   WORD   offsety = (screen->Height/2);
  71.   double temp;
  72.   double angle=0;
  73.   ULONG  colour;
  74.   double Z2,X2,Y2;
  75.   LONG   scale=32;
  76.   UWORD  anglex=0,angley=0,anglez=0;
  77.   double longtitude;
  78.   double latitude;
  79.  
  80.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA);
  81.   sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  82.   cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  83.  
  84.   /* First calculate the X, Y and Z coordinates of our object */
  85.  
  86.   for (i=0; i<AMTDOTS; i++) {
  87.     longtitude = FastRandom(100);
  88.     latitude   = FastRandom(100);
  89.     object[i].X  = sin(longtitude)*cos(latitude);
  90.     object[i].Y  = sin(longtitude)*sin(latitude);
  91.     object[i].Z  = cos(longtitude);
  92.   }
  93.  
  94.   /* Now generate our cosine and sinus tables */
  95.  
  96.   for (i=0; i<360; i++) {
  97.     cosine[i] = cos(angle);
  98.     sine[i]   = sin(angle);
  99.     angle    += 0.25;
  100.   }
  101.  
  102.   /* Commence our main loop */
  103.  
  104.   do
  105.   {
  106.     Query(jport1);
  107.     scale += jport1->YChange;
  108.     if (scale < 1) scale = 1;
  109.     if (scale > 100) scale = 100;
  110.  
  111.     Clear(screen->Bitmap);
  112.  
  113.     for (i=0; i<AMTDOTS; i++) {
  114.  
  115.       X2 = object[i].X;
  116.       Y2 = object[i].Y;
  117.       Z2 = object[i].Z;
  118.  
  119.       /* Rotate the X axis */
  120.  
  121.       temp = Z2;
  122.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  123.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  124.  
  125.       /* Rotate the Y axis */
  126.  
  127. //      temp = Z2;
  128. //      Z2 = Z2*cosine[angley] - X2*sine[angley];
  129. //      X2 = X2*cosine[angley] + temp*sine[angley];
  130.  
  131.       /* Rotate the Z axis */
  132.  
  133. //      temp = X2;
  134. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  135. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  136.  
  137.       /* Calculate colour based on Z position (-1 < Z < +1) */
  138.  
  139.       colour = ((Z2+1)*screen->Bitmap->AmtColours)/2;
  140.  
  141.       /* Finally scale the (x,y) coordinates to enlarge or shrink the object */
  142.  
  143.       X2 *= scale;
  144.       Y2 *= scale;
  145.  
  146.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  147.     }
  148.     anglex++; if (anglex >= 360) anglex = 0;
  149.     angley++; if (angley >= 360) angley = 0;
  150.     anglez++; if (anglez >= 360) anglez = 0;
  151.  
  152.     WaitVBL();
  153.     SwapBuffers(screen);
  154.   } while(!(jport1->Buttons & JD_LMB));
  155.  
  156.   FreeMemBlock(object);
  157.   FreeMemBlock(sine);
  158.   FreeMemBlock(cosine);
  159. }
  160.  
  161.